﻿using System;
using System.Collections.Generic;
using System.Text;
using Kitware.VTK;

using System.ComponentModel;

namespace Fu3DEngine
{
    [Serializable]
   public abstract  class SceneBase

    {
        public SceneBase(string id)
        {
            this.sid = id;
            this.desc = this.sid;
        }


        //基类属性
        [Browsable(false)]
        public string sid { get; set; }

        [Category ("底层场景"),DisplayName("节点名称")]
        public string desc { get; set; }

        //一些常用的属性
        TPoint m_BackColor = new TPoint(0.9,0.9,0.8);


        //基类事件关系
        public event NodeEventA NodeAdded;
        public event NodeEventR NodeRemoved;


     public   Dictionary <string ,NodeBase> nodeDic = new Dictionary <string,NodeBase>();

        [NonSerialized]
      public  vtkRenderWindow m_vtkRenderWindow;
        [NonSerialized]
      public  vtkRenderer m_vtkRenderer;
        [NonSerialized]
        public vtkRenderWindowInteractor m_vtkInteractor;
       public void InitScene(RenderWindowControl rwc)
       {

           m_vtkRenderWindow = rwc.RenderWindow;
           m_vtkRenderer = rwc.RenderWindow.GetRenderers().GetFirstRenderer();


           m_vtkInteractor = vtkRenderWindowInteractor.New();
           m_vtkRenderWindow.SetInteractor(m_vtkInteractor);



           vtkInteractorStyle style = vtkInteractorStyleTrackballCamera.New();
           m_vtkRenderWindow.GetInteractor().SetInteractorStyle(style);


          

          



           //m_vtkInteractor = m_vtkRenderWindow.GetInteractor();


           //开始从新构建所有的节点，与属性关系
           m_vtkRenderer.SetBackground(m_BackColor .x,m_BackColor .y,m_BackColor .z);

           

       }


       public  virtual  void Run()
       {
           foreach (KeyValuePair <string,NodeBase> item in nodeDic)
           {
               item.Value.Run();
           }
 
       }


  

       ////类似于构造函数，但是可以在外部调用
       //public abstract bool CreateScene(SceneData data);


       public void UnloadAllNodes()
       {
           foreach (KeyValuePair <string,NodeBase > item in nodeDic )
           {
               item.Value.Unload();
               
           }
           nodeDic.Clear();
       }

       public void Reload()
       {
           foreach (KeyValuePair <string ,NodeBase > item in nodeDic)
           {
               item.Value.ReLoad();
               
           }
 
       }

       public NodeBase GetNode(string nid)
       {
           if (nodeDic.ContainsKey(nid))
           {
               NodeBase nb = nodeDic[nid];

               return nb;


           }
           else
           {
               return null;
           }
       }


       public void AddNode(NodeBase nb)
       {
           if (nb == null)
           {
               return;
           }


           nb.SParent = this;

           nodeDic.Add(nb.nid, nb);

           //激活事件
           if (NodeAdded != null)
           {
               NodeAdded(nb);
           }


       }

       public void RemoveNode(string nid)
       {
           if (nodeDic.ContainsKey (nid))
           {
               nodeDic[nid].SParent = null;
               nodeDic.Remove(nid);
           }

           if (NodeRemoved!=null)
           {

               NodeRemoved(nid);
               
           }
       }
    }
}
